package com.ajavaer.framework.generator.model;

import com.ajavaer.framework.common.spring.SpringContext;
import com.ajavaer.framework.common.tools.StringTools;
import com.ajavaer.framework.common.validate.annotation.CheckField;
import com.ajavaer.framework.config.GeneratorConfig;
import com.ajavaer.framework.generator.type.JpaNamingType;
import org.hibernate.cfg.ImprovedNamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 类信息
 */
public class ClassInfo {
    /**
     * 表名
     */
    @CheckField(notEmpty = true, message = "表名不能为空")
    private String tableName;
    /**
     * 类名
     */
    @CheckField(notEmpty = true, message = "类名不能为空")
    private String className;
    /**
     * 主键类型,Integer,Long(默认),String
     */
    private String pkType = "Long";
    /**
     * 类备注
     */
    private String classComment;
    @CheckField(notEmpty = true, message = "po包路径不能为空")
    private String poPackage;
    private String dtoPackage;
    private String dtoMapperPackage;
    @CheckField(notEmpty = true, message = "dao包路径不能为空")
    private String daoPackage;
    @CheckField(notEmpty = true, message = "service包路径不能为空")
    private String servicePackage;
    private String controllerPackage;
    @CheckField(notEmpty = true, message = "父类不能为空")
    private String superPoClass;
    private String superPoClassName;
    /**
     * 字段命名规则,默认驼峰.
     */
    private JpaNamingType jpaNamingType = JpaNamingType.hump;

    private List<ClassField> classFields = new ArrayList<>();

    private ClassImport classImport = new ClassImport();

    public JpaNamingType getJpaNamingType() {
        return jpaNamingType;
    }

    public void setJpaNamingType(JpaNamingType jpaNamingType) {
        this.jpaNamingType = jpaNamingType;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getClassComment() {
        return classComment;
    }

    public void setClassComment(String classComment) {
        this.classComment = classComment;
    }

    public ClassImport getClassImport() {
        return classImport;
    }

    public void setClassImport(ClassImport classImport) {
        this.classImport = classImport;
    }

    public String getPoPackage() {
        return poPackage;
    }

    public void setPoPackage(String poPackage) {
        this.poPackage = poPackage;
    }

    public String getDaoPackage() {
        return daoPackage;
    }

    public void setDaoPackage(String daoPackage) {
        this.daoPackage = daoPackage;
    }

    public String getServicePackage() {
        return servicePackage;
    }

    public void setServicePackage(String servicePackage) {
        this.servicePackage = servicePackage;
    }

    public List<ClassField> getClassFields() {
        return classFields;
    }

    public String getSuperPoClass() {
        return superPoClass;
    }

    public void setSuperPoClass(String superPoClass) {
        this.superPoClass = superPoClass;
    }

    public void setClassFields(List<ClassField> classFields) {
        this.classFields = classFields;
    }

    public String getSuperPoClassName() {
        return superPoClassName;
    }

    public void setSuperPoClassName(String superPoClassName) {
        this.superPoClassName = superPoClassName;
    }

    public String getControllerPackage() {
        return controllerPackage;
    }

    public void setControllerPackage(String controllerPackage) {
        this.controllerPackage = controllerPackage;
    }

    public String getPkType() {
        return pkType;
    }

    public void setPkType(String pkType) {
        this.pkType = pkType;
    }

    public String getDtoPackage() {
        return dtoPackage;
    }

    public String getDtoMapperPackage() {
        return dtoMapperPackage;
    }

    public void setDtoMapperPackage(String dtoMapperPackage) {
        this.dtoMapperPackage = dtoMapperPackage;
    }

    public void setDtoPackage(String dtoPackage) {
        this.dtoPackage = dtoPackage;
    }

    public void process() {
        GeneratorConfig config = SpringContext.getBean(GeneratorConfig.class);
        if (StringTools.isNotBlank(config.getPkType())) {
            this.pkType = config.getPkType();
        }
        if (this.jpaNamingType == JpaNamingType.hump) {
            if (StringTools.isNotBlank(this.className) && StringTools.isBlank(this.tableName)) {
                this.tableName = ImprovedNamingStrategy.INSTANCE.classToTableName(config.getTablePrefix() + this.className);
            } else if (StringTools.isNotBlank(this.tableName) && StringTools.isBlank(this.className)) {
                this.className = StringTools.underlineToHump(this.tableName.toLowerCase().replace(config.getTablePrefix(), ""), true);
            }
        } else {
            if (StringTools.isNotBlank(this.className) && StringTools.isBlank(this.tableName)) {
                this.tableName = this.className;
            } else if (StringTools.isNotBlank(this.tableName) && StringTools.isBlank(this.className)) {
                this.className = this.tableName;
            }
        }
        this.className = StringTools.firstLetterToUpper(this.className);
        if (superPoClass == null) {
            this.superPoClass = config.getSuperPoClass();
        }
        int index = superPoClass.lastIndexOf(".");
        this.superPoClassName = superPoClass.substring(index + 1);
        if (classFields != null) {
            for (ClassField classField : classFields) {
                if (classField.getHasDecimal()) {
                    classImport.setBigdecimal(true);
                }
                if (classField.getHasDate()) {
                    classImport.setDate(true);
                    classImport.setJsonformat(true);
                }
                if (classField.getHasDateTime()) {
                    classImport.setDatetime(true);
                    classImport.setJsonformat(true);
                }
                if (classField.getHasTimestamp()) {
                    classImport.setTimestamp(true);
                }
                if(classField.getHasCheckfield()){
                    classImport.setCheckfield(true);
                }
            }
        }
    }

}
